package algorithms.greedy.meet;

import java.util.Arrays;
import java.util.List;

/**
 * 贪心算法：安排会议
 */
public class MeetTest {
    public static void main(String[] args) {
        Meet[] meets = {new Meet("早上6点", 6, 7),
                new Meet("早上8点", 8, 14),
                new Meet("早上9点", 9, 10),
                new Meet("早上10点", 10, 12),
                new Meet("下午14点", 14, 17),
                new Meet("下午15点", 15, 17),
                new Meet("下午15点", 17, 18)};

        // 排序测试
        //List<Meet> meetList = Arrays.asList(meets);
        //meetList.stream().sorted((o1,o2)->{return o1.getEnd() - o2.getEnd();})
        //        .forEach(System.out::println);

        System.out.println(apply(meets,6,18));

    }

    /**
     * 贪心策略：以结束时间优先安排
     *
     * @param meets
     * @param start
     * @param end
     * @return
     */
    public static int apply(Meet[] meets,int start,int end) {
        // 安排的会议数量
        int count = 0;
        if (meets == null) {
            return count;
        }
        // 按照结束时间进行排序
        Arrays.sort(meets,(o1,o2)->{return o1.getEnd() - o2.getEnd() ;});
        System.out.println(Arrays.toString(meets));
        int endTime = start;

        for (int i = 0; i < meets.length; i++) {
            if (endTime <= meets[i].getStart()) {
                count++;
                endTime = meets[i].getEnd();
                System.out.println("安排会议："+meets[i].getName()+
                        "时间："+meets[i].getStart()+
                        " - "+meets[i].getEnd());
            }
        }
        return count;
    }

}



class Meet {
    private String name;
    private int start;
    private int end;

    public Meet(String name,int start, int end) {
        this.name = name;
        this.start = start;
        this.end = end;
    }

    public int getStart() {
        return start;
    }

    public int getEnd() {
        return end;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Meet{" +
                "name='" + name + '\'' +
                ", start=" + start +
                ", end=" + end +
                '}';
    }
}

